"""
本模块定义了基本的设置
"""
import sys
import logging

from dotenv import dotenv_values, load_dotenv
from pydantic import BaseModel

def init():
    try:
        if not load_dotenv():
            print("Plese create a file name .env in your root path")
            print("Or try run the setup.py by `python setup.py`")
            sys.exit()

        env = dotenv_values()
        FlaskConfig = FlaskModel(
            PORT = env['FLASK_RUN_PORT'],
            DEBUG = env['FLASK_DEBUG'],
            ENV = env['FLASK_ENV'],
        )
        DBConfig = DBModel(
            host = env['DB_HOST'],
            port = env['DB_PORT'],
            user = env['DB_USER'],
            password = env['DB_PASSWORD'],
            db = env['DB_NAME']
        )
        output = f"{env['LOG_PATH']}/{env['LOG_FILE_NAME']}.log"
        logging.basicConfig(filename=output, encoding='utf8', level=logging.DEBUG)
        return Config(FlaskConfig, DBConfig, output)

    except Exception as e:
        print("Config load Error")
        print(type(e),e)
        sys.exit()
    finally:
        pass

# TODO: ADD VALIDATE CHEACK

class FlaskModel(BaseModel):
    """FLASK配置"""
    PORT:int = 8080
    DEBUG:bool = False
    ENV:str = 'development'

class DBModel(BaseModel):
    """数据库配置"""
    host: str
    port: int
    user: str
    password: str
    db: str

class Config():
    """全局配置"""
    FlaskConfig: FlaskModel
    DBConfig: DBModel
    LogPath: str

    def __init__(self, FConfig, Dconfig, LogPath) -> None:
        self.FlaskConfig = FConfig
        self.DBConfig = Dconfig
        self.LogPath = LogPath